//
//  XHHuiMinViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-7-26.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "XHHuiMinViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "XHShengHuoTongViewController.h"
#import "XHTableViewHeaderView.h"
#import "XHZhangShangViewModel.h"
#import <MBProgressHUD.h>
#import "MainEnterViewController.h"
#import "dangeViewController.h"
#import  "guangfangfabuViewController.h"
#import "HUIMINGMODEL.h"


@interface XHHuiMinViewController ()<UITableViewDelegate>
@property (nonatomic ,weak) IBOutlet UIButton *shenghuotong;
@property (nonatomic ,strong)  UITableView *tableView;
@property (nonatomic ,strong) HUIMINGMODEL *viewModel;
@property (nonatomic ,strong) MBProgressHUD *HUD;
-(IBAction)select:(id)sender;
@end

@implementation XHHuiMinViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"HuiMinView" owner:self options:nil];
        UIView *view = [array objectAtIndex:0];
        CGRect rect = self.view.frame;
        CGFloat oY = kIOS_VERSION<7.0?44:64;
        [view setFrame:CGRectMake(0, oY, rect.size.width, rect.size.height-oY)];
        [self.view addSubview:view];
        self.titleLable.text = @"惠民生活";
        @weakify(self);
        [[self.leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
           // [self.viewModel canall];
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [self.leftButton setImage:[UIImage imageNamed:@"icon_back@2x"] forState:UIControlStateNormal];
        self.rightButton.hidden = YES;
        [self.view bringSubviewToFront:self.navBar];
        _shenghuotong.layer.masksToBounds = YES;
        _shenghuotong.layer.cornerRadius = 20.0f;
        self.viewModel = [[HUIMINGMODEL alloc]  init:nil huiming:@"4"];
        NSUserDefaults* fefe = [NSUserDefaults standardUserDefaults];
        
        self.viewModel.titleArray = [fefe objectForKey:@"惠民栏目"];
        self.viewModel.dataArray = [fefe objectForKey:@"惠民新闻"];

        self.tableView = [[UITableView alloc] init];
        self.tableView.frame = CGRectMake(10, oY+5+40+10, 300, self.view.frame.size.height-oY-5-50);
        self.tableView.dataSource = self.viewModel;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
        
        
        [RACObserve(self.viewModel, hasLoad) subscribeNext:^(id x) {
            if ([x intValue]) {
                @strongify(self);
                [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:YES];
            }
        }];
        self.HUD = [[MBProgressHUD alloc] initWithView:view];
        [self.view addSubview:self.HUD];
        
        self.HUD.labelText = @"正在加载";
        self.HUD.dimBackground = NO;
        [self.HUD show:YES];
        if ([self.viewModel.titleArray count] > 0)
        {
            [self refresh];
        }

    }
    return self;

}

- (void)refresh
{
    [self.tableView reloadData];
    [self.HUD hide:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* string = [[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    NSLog(@"string = %@",string);
    dangeViewController* viewContr = [[dangeViewController alloc] init];
        viewContr.newdId = [[[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"id"] stringValue];
        NSLog(@"id = %@",viewContr.newdId);
        viewContr.titleString = @"惠民生活";
    viewContr.huimingbiaoti  = string;
        [self.navigationController pushViewController:viewContr animated:YES];
    butoncl* bl = [[butoncl alloc] init];
    //NSString* str = [viewControoler.yinitData[@"id"] stringValue];
    [bl postString:@"001" fff:viewContr.newdId];


}
- (IBAction)select:(id)sender
{
    XHShengHuoTongViewController *vc = [[XHShengHuoTongViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
